package com.outsourcing.bottle.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.text.Html;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ext.SatelliteMenu;
import android.view.ext.SatelliteMenu.SateliteClickedListener;
import android.view.ext.SatelliteMenuItem;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.outsourcing.bottle.BasicActivity;
import com.outsourcing.bottle.R;
import com.outsourcing.bottle.db.LoginUserInfoTable;
import com.outsourcing.bottle.db.PushNoticeInfoTable;
import com.outsourcing.bottle.domain.FriendFeedEntry;
import com.outsourcing.bottle.domain.FriendFeedInfo;
import com.outsourcing.bottle.domain.LoginUserInfoEntry;
import com.outsourcing.bottle.domain.PushNoticeInfo;
import com.outsourcing.bottle.domain.ResponseResult;
import com.outsourcing.bottle.domain.UrlConfig;
import com.outsourcing.bottle.remoteimage.RemoteImageView;
import com.outsourcing.bottle.util.AppContext;
import com.outsourcing.bottle.util.HttpUtils;
import com.outsourcing.bottle.util.ImageLoader;
import com.outsourcing.bottle.util.ServiceUtils;
import com.outsourcing.bottle.util.TextUtil;
import com.outsourcing.bottle.util.UICore;
import com.outsourcing.bottle.widget.CustomListView;

/**
 * 
 * @author 06peng
 * 
 */
public class FriendFeedActivity extends BasicActivity implements
		OnClickListener, Callback, OnScrollListener {

	private static final int INIT_FRIEND_FEED = 0;
	private static final int GET_MORE_DATA = 1;
	private static final int ERROR = 2;
	private static final String TAG = FriendFeedActivity.class.getSimpleName();
	private static final int EXEU_GET_FRIEND_NOTHING = 3;

	private final int user_baseinfo_notfull = 4;
	private final int complete_info = 5;

	private ImageView backView;
	private ImageView addView;
	private TextView titleView;
	private CustomListView refreshView;

	private final int menu_uploadphoto = 15;
	private final int menu_exprofile = 14;
	private final int menu_exphoto = 13;
	private final int menu_gainbt = 12;
	private final int menu_sendbt = 11;

	private ImageLoader imageLoader;
	private Handler handler;
	private int page = 1;
	private List<FriendFeedEntry> friendFeedList;
	private FriendFeedAdapter friendAdapter = null;

	private int visibleLastIndex = 0; // 最后的可视项索引
	public int visibleItemCount; // 当前窗口可见项总数
	private View loadMoreView;
	
//	private ImageView messageView;
	private LinearLayout bottomLayout;
	private ImageView avatarView;
	private TextView contentView;
	private TextView btnCount;
	private int newNoticeCount;
	private int newMessageCount;
	private int newBottleFeedCount;
	private int newExchangeFeedCount;
	private int newMaybeKownCount;
	private String message_content;
	private String message_avatar;
	private ImageLoader userImageLoader;
	private PushBroadcastReceiver receiver;
	private PushNoticeInfo pushInfo;
	
	private String choices[];

	@Override
	protected void onCreate(Bundle paramBundle) {
		super.onCreate(paramBundle);
		setContentView(R.layout.friend_feeds_layout);
		handler = new Handler(this);
		imageLoader = new ImageLoader(this, AppContext.BottleTimelineIcon);
		userImageLoader = new ImageLoader(this, AppContext.UserAvatarIcon);
		initWidget();
		initSateMenu();
		UICore.eventTask(this, this, INIT_FRIEND_FEED, "", null);
	}

	private void initWidget() {
		backView = (ImageView) findViewById(R.id.topbar_back);
		backView.setOnClickListener(this);
		addView = (ImageView) findViewById(R.id.topbar_menu);
		addView.setOnClickListener(this);
		titleView = (TextView) findViewById(R.id.topbar_title);
		titleView.setVisibility(View.VISIBLE);
		
//		messageView = (ImageView) findViewById(R.id.notice_message_count);
//		messageView.setOnClickListener(this);
		
		refreshView = (CustomListView) findViewById(R.id.list_view);
		refreshView.setonRefreshListener(new CustomListView.OnRefreshListener() {
			
			@Override
			public void onRefresh() {
				page = 1;
				UICore.eventTask(FriendFeedActivity.this, FriendFeedActivity.this, INIT_FRIEND_FEED, null, null);
			}
		});
		loadMoreView = LayoutInflater.from(this).inflate(R.layout.refreshable_list_footer, null);
		loadMoreView.setVisibility(View.GONE);
		refreshView.addFooterView(loadMoreView);
		refreshView.setOnScrollListener(this);
		refreshView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				if (arg2 == friendAdapter.getCount() + 1) {
					return;
				}
				choices = null;
				final FriendFeedEntry entry = friendFeedList.get(arg2 - 1);
				if (entry.getFeed_picid() != 0) {
					if (entry.getFeed_picid() != 0 && entry.getFeed_related_uid() != 0) {
						choices = new String[2];
						choices[0] = getString(R.string.user_space_bottle_view_photo);
						choices[1] = getString(R.string.user_info_view) + entry.getFeed_related_nickname() + getString(R.string.user_info_space);
						chooseAction(choices, entry);
					} else if (entry.getFeed_picid() != 0) {
						choices = new String[1];
						choices[0] = getString(R.string.user_space_bottle_view_photo);
						chooseAction(choices, entry);
					} else if (entry.getFeed_related_uid() != 0) {
						if (TextUtil.notEmpty(entry.getFeed_related_nickname())) {
							choices = new String[1];
							choices[0] = getString(R.string.user_info_view) + entry.getFeed_related_nickname() + getString(R.string.user_info_space);
							chooseAction(choices, entry);
						}
					}
				} else {
					if (entry.getFeed_related_uid() != 0) {
						if (TextUtil.notEmpty(entry.getFeed_related_nickname())) {
							choices = new String[1];
							choices[0] = getString(R.string.user_info_view) + entry.getFeed_related_nickname() + getString(R.string.user_info_space);
							chooseAction(choices, entry);
						}
					}
				}
			}
		});
		
		bottomLayout = (LinearLayout) findViewById(R.id.notice_bottom_layout);
		bottomLayout.setOnClickListener(this);
		avatarView = (ImageView) findViewById(R.id.notice_avatar);
		contentView = (TextView) findViewById(R.id.notice_text);
		btnCount = (TextView) findViewById(R.id.notice_number);
//		messageView = (ImageView) findViewById(R.id.notice_message_count);
//		messageView.setOnClickListener(this);
//		messageView.setVisibility(View.VISIBLE);
	}

	@Override
	public boolean handleMessage(Message msg) {
		switch (msg.what) {
		case INIT_FRIEND_FEED:
			friendAdapter = new FriendFeedAdapter();
			refreshView.setAdapter(friendAdapter);
			pushNoticeUpdateUI();
			receiver = new PushBroadcastReceiver();
			IntentFilter filter = new IntentFilter();
			filter.addAction("push");
			registerReceiver(receiver, filter);
			break;
		case ERROR:
			onToast((String) msg.obj);
			break;
		case GET_MORE_DATA:
			friendAdapter.notifyDataSetChanged();
			// refreshView.onRefreshComplete();
			break;
		case user_baseinfo_notfull:
			showChoseMes(getString(R.string.exchange_complete_profile_tips),
					complete_info);
			break;
		case EXEU_GET_FRIEND_NOTHING:
			loadMoreView.setVisibility(View.GONE);
			break;
		default:
			break;
		}
		return false;
	}

	@Override
	public void onClick(View v) {
		if (v == backView) {
			finish();
		} else if (v == addView) {
//			Intent intent = new Intent(this, MoreActivity.class);
//			startActivity(intent);
			Intent intent = new Intent(FriendFeedActivity.this, HomeActivity.class);
			intent.putExtra("currentItem", 1);
			startActivity(intent);
			finish();	
		} 
//		else if (v == messageView) {
//			doMessageAction();
//		} 
		else if (v == bottomLayout) {
			doFeedsAction();
		}
	}

	@Override
	public void execute(int mes, Object obj) {
		switch (mes) {
		case INIT_FRIEND_FEED:
			getFriendFeeds(page);
			break;
		case GET_MORE_DATA:
			getMoreFriendFeeds(page);
			break;
		default:
			break;
		}
	}

	private void getFriendFeeds(int page) {
		StringBuffer url = new StringBuffer(UrlConfig.get_my_feedslist);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&count=" + 20);
		url.append("&page=" + page);
		try {
			String result = HttpUtils.doGet(url.toString());
			if (result != null) {
				Gson gson = new Gson();
				FriendFeedInfo info = gson.fromJson(result, FriendFeedInfo.class);
				ResponseResult res = info.getResults();
				if (res.getAuthok() == 1) {
					friendFeedList = info.getFeeds_list();
					PushNoticeInfoTable table = new PushNoticeInfoTable();
					pushInfo = table.getPushNoticeInfo(AppContext.getInstance().getLogin_uid());
					if (null!=pushInfo) {
						newBottleFeedCount = pushInfo.getNew_btfeed_count();
						newExchangeFeedCount = pushInfo.getNew_exfeed_count();
						newMessageCount = pushInfo.getNew_message_count();
						newNoticeCount = pushInfo.getNew_notice_count();
						message_content = pushInfo.getMessage_content();
						message_avatar = pushInfo.getMessage_avatar();
						newMaybeKownCount = pushInfo.getNew_maybeknow_count();
					}
					Message message = Message.obtain(handler);
					message.what = INIT_FRIEND_FEED;
					handler.sendMessage(message);
				} else {
					Message message = handler.obtainMessage(ERROR, info.getResults().getErrmsg());
					handler.sendMessage(message);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void getMoreFriendFeeds(int page) {
		StringBuffer url = new StringBuffer(UrlConfig.get_my_feedslist);
		url.append("?clitype=2");
		url.append("&language=" + AppContext.language);
		url.append("&login_uid=" + AppContext.getInstance().getLogin_uid());
		url.append("&login_token=" + AppContext.getInstance().getLogin_token());
		url.append("&count=" + 20);
		url.append("&page=" + page);
		try {
			String result = HttpUtils.doGet(url.toString());
			if (result != null) {
				Gson gson = new Gson();
				FriendFeedInfo info = gson.fromJson(result, FriendFeedInfo.class);
				ResponseResult res = info.getResults();
				if (res.getAuthok() == 1) {
					List<FriendFeedEntry> moreFriendFeedList = info.getFeeds_list();
					if (moreFriendFeedList != null && !moreFriendFeedList.isEmpty()) {
						friendFeedList.addAll(moreFriendFeedList);
						handler.sendEmptyMessage(GET_MORE_DATA);
					} else {
						Message message = handler.obtainMessage(EXEU_GET_FRIEND_NOTHING);
						handler.sendMessage(message);
					}
				} else {
					Message message = handler.obtainMessage(ERROR, info.getResults().getErrmsg());
					handler.sendMessage(message);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	class ViewHolder {
		RemoteImageView avatar;
		TextView content;
		TextView timeView;
		ImageView image;
		int flag = -1;
	}
	ViewHolder holder = null;
	public class FriendFeedAdapter extends BaseAdapter {
		
//		private String choices[];

		@Override
		public int getCount() {
			return friendFeedList == null ? 0 : friendFeedList.size();
		}

		@Override
		public Object getItem(int position) {
			return friendFeedList == null ? null : friendFeedList.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			if (convertView == null
					|| ((ViewHolder) convertView.getTag()).flag != position) {

				holder = new ViewHolder();
				holder.flag = position;

				convertView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.friend_feed, null);
				holder.content = (TextView) convertView.findViewById(R.id.feed_content);
				holder.timeView = (TextView) convertView.findViewById(R.id.feed_time);
				holder.avatar = (RemoteImageView) convertView.findViewById(R.id.avatar);
				holder.image = (ImageView) convertView.findViewById(R.id.feed_image);
				convertView.setTag(holder);
				
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			final FriendFeedEntry entry = friendFeedList.get(position);
			if (entry != null) {
				ServiceUtils.dout(TAG + " entry.getFeed_avatar():" + entry.getFeed_avatar());
				holder.avatar.setDefaultImage(R.drawable.avatar_default_big);
				holder.avatar.setImageUrl(entry.getFeed_avatar());
				String nickname = "<font color=#419AD9>" + entry.getFeed_nickname() + "</font>";
				holder.content.setText(Html.fromHtml(nickname + "&nbsp;" + (entry.getIsnew() == 1 ? "<font color=red>" + TextUtil.htmlEncode(entry.getFeed_content()) + "</font>" : TextUtil.htmlEncode(entry.getFeed_content()))));
				holder.timeView.setText(entry.getFeed_time());
				if (TextUtil.notEmpty(entry.getFeed_smallpic())) {
					imageLoader.DisplayImage(entry.getFeed_smallpic(), holder.image, R.drawable.album_nophoto);
					holder.image.setVisibility(View.VISIBLE);
					((RelativeLayout) convertView.findViewById(R.id.rl_bottle_content_photo)).setVisibility(View.VISIBLE);
				} else {
					((RelativeLayout) convertView.findViewById(R.id.rl_bottle_content_photo)).setVisibility(View.GONE);
				}
				holder.avatar.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						Intent spaceIntent = new Intent(FriendFeedActivity.this, UserSpaceActivity.class);
						spaceIntent.putExtra("ouid", entry.getFeed_uid());
						startActivity(spaceIntent);
					}
				});
			}
			return convertView;
		}
	}


	private void initSateMenu() {
		SatelliteMenu menu = (SatelliteMenu) findViewById(R.id.menu);
		menu.setVisibility(View.VISIBLE);
		if (CanvasWidth >= 480) {
			menu.setSatelliteDistance(200);
		} else {
			menu.setSatelliteDistance(100);
		}
		List<SatelliteMenuItem> items = new ArrayList<SatelliteMenuItem>();
		items.add(new SatelliteMenuItem(menu_exprofile, R.drawable.menu_exprofile));
		items.add(new SatelliteMenuItem(menu_exphoto, R.drawable.menu_exphoto));
		items.add(new SatelliteMenuItem(menu_gainbt, R.drawable.menu_gainbt));
		items.add(new SatelliteMenuItem(menu_sendbt, R.drawable.menu_sendbt));
		items.add(new SatelliteMenuItem(menu_uploadphoto, R.drawable.menu_uploadphoto));
		menu.addItems(items);
		menu.setOnItemClickedListener(new SateliteClickedListener() {

			public void eventOccured(int id) {
				switch (id) {
				case menu_sendbt: // 扔瓶子
					new Thread() {
						public void run() {
							try {
								Thread.sleep(500);
								Intent throwIntent = new Intent(
										FriendFeedActivity.this,
										ChooseBottleActivity.class);
								throwIntent.putExtra("type", "throw");
								startActivity(throwIntent);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						};
					}.start();
					break;
				case menu_gainbt: // 捞瓶子
					new Thread() {
						public void run() {
							try {
								Thread.sleep(500);
								Intent tryIntent = new Intent(
										FriendFeedActivity.this,
										ChooseBottleActivity.class);
								tryIntent.putExtra("type", "try");
								startActivity(tryIntent);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						};
					}.start();
					break;
				case menu_exphoto: // 交换照片
					new Thread() {
						public void run() {
							try {
								Thread.sleep(500);
								Intent exchangeInfoIntent = new Intent(
										FriendFeedActivity.this,
										ChooseFriendActivity.class);
								exchangeInfoIntent
										.putExtra(
												"choose_friend_type",
												AppContext.CHOOSE_FRIEND_EXCHANGE_PHOTO_TYPE);
								startActivity(exchangeInfoIntent);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						};
					}.start();
					break;
				case menu_exprofile: // 交换资料
					new Thread() {
						public void run() {
							try {
								Thread.sleep(500);
								LoginUserInfoTable userTable = new LoginUserInfoTable();
								LoginUserInfoEntry userEntry = userTable
										.getLoginUserInfo(AppContext
												.getInstance().getLogin_uid());
								if (userEntry.getBasicinfo_fullfill() == 0) {
									handler.sendEmptyMessage(user_baseinfo_notfull);
								} else {
									Intent exchangeFileIntent = new Intent(
											FriendFeedActivity.this,
											ChooseFriendActivity.class);
									exchangeFileIntent
											.putExtra(
													"choose_friend_type",
													AppContext.CHOOSE_FRIEND_EXCHANGE_INFO_TYPE);
									startActivity(exchangeFileIntent);
								}
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						};
					}.start();
					break;
				case menu_uploadphoto:
					new Thread() {
						public void run() {
							try {
								Thread.sleep(500);
								Intent intent = new Intent(FriendFeedActivity.this, ExpandEditActivity.class);
								Bundle extras = new Bundle();
								extras.putString("from_type", AppContext.UPLOAD_PHOTO);
								extras.putInt("albumid", 0);
								extras.putInt("ouid", AppContext.getInstance().getLogin_uid());
								intent.putExtras(extras);
								startActivity(intent);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						};
					}.start();
					break;
				default:
					break;
				}
			}
		});
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		refreshView.setFirstVisiableItem(firstVisibleItem);
		this.visibleItemCount = visibleItemCount;
		visibleLastIndex = firstVisibleItem + visibleItemCount;
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		int itemsLastIndex = friendAdapter.getCount() + 1; // 数据集最后一项的索引
		int lastIndex = itemsLastIndex + 1; // 加上底部的loadMoreView项 和 head项
		ServiceUtils.dout(TAG + " visibleLastIndex:" + visibleLastIndex);
		ServiceUtils.dout(TAG + "        lastIndex:" + lastIndex);
		if (scrollState == OnScrollListener.SCROLL_STATE_IDLE
				&& visibleLastIndex == lastIndex) {
			// 如果是自动加载,可以在这里放置异步加载数据的代码
			loadMoreView.setVisibility(View.VISIBLE);
			page++;
			UICore.eventTask(FriendFeedActivity.this, FriendFeedActivity.this, GET_MORE_DATA, null, null);
		}
	}

	public void doMessageAction() {
		
		final Context dialogContext = new ContextThemeWrapper(this,
				android.R.style.Theme_Light);
		String[] choices = new String[2];
		if (newNoticeCount > 0) {
			choices[0] = newNoticeCount + getString(R.string.bottle_txt_notice_list);
		} else {
			choices[0] = getString(R.string.bottle_txt_notice_list);
		}
		if (newMessageCount > 0) {
			choices[1] = newMessageCount + getString(R.string.bottle_txt_message_list);
		} else {
			choices[1] = getString(R.string.bottle_txt_message_list);
		}
	
		final ListAdapter adapter = new ArrayAdapter<String>(dialogContext,
				android.R.layout.simple_list_item_1, choices);

		final AlertDialog.Builder builder = new AlertDialog.Builder(
				dialogContext);
		builder.setTitle(R.string.system_info);
		builder.setSingleChoiceItems(adapter, -1,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						if (which == 0) {
							Intent intent = new Intent(FriendFeedActivity.this, NoticeListActivity.class);
							startActivity(intent);
						} else {
							Intent intent = new Intent(FriendFeedActivity.this, MessageListActivity.class);
							startActivity(intent);
						}
					}
				});
		builder.create().show();
	}
	
	public void doFeedsAction() {
		final Context dialogContext = new ContextThemeWrapper(this, android.R.style.Theme_Light);
		final List<String> choices = new ArrayList<String>(); 
		if (newBottleFeedCount > 0) {
			choices.add(newBottleFeedCount + getString(R.string.bottle_txt_new_bottle_feed));
		}
		if (newExchangeFeedCount > 0) {
			choices.add(newExchangeFeedCount + getString(R.string.bottle_txt_new_exchange_feed));
		}
		if (newNoticeCount > 0) {
			choices.add(newNoticeCount + getString(R.string.bottle_txt_notice_list));
		}
		if (newMessageCount > 0) {
			choices.add(newMessageCount + getString(R.string.bottle_txt_message_list));
		}
		if (newMaybeKownCount > 0) {
			choices.add(newMaybeKownCount + getString(R.string.bottle_txt_new_maybe_kown));
		}
		final ListAdapter adapter = new ArrayAdapter<String>(dialogContext, android.R.layout.simple_list_item_1, choices);
		final AlertDialog.Builder builder = new AlertDialog.Builder(dialogContext);
		builder.setTitle(R.string.system_info);
		builder.setSingleChoiceItems(adapter, -1,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						if (choices.get(which).indexOf(getString(R.string.bottle_txt_new_bottle_feed)) != -1) {
							Intent intent = new Intent(FriendFeedActivity.this, HomeActivity.class);
							AppContext.getInstance().setCurrentItem(1);
							startActivity(intent);
						} else if (choices.get(which).indexOf(getString(R.string.bottle_txt_new_exchange_feed)) != -1) {
							Intent intent = new Intent(FriendFeedActivity.this, HomeActivity.class);
							AppContext.getInstance().setCurrentItem(0);
							startActivity(intent);
						} else if (choices.get(which).indexOf(getString(R.string.bottle_txt_notice_list)) != -1) {
							Intent intent = new Intent(FriendFeedActivity.this, NoticeListActivity.class);
							startActivity(intent);
						} else if (choices.get(which).indexOf(getString(R.string.bottle_txt_message_list)) != -1) {
							Intent intent = new Intent(FriendFeedActivity.this, MessageListActivity.class);
							startActivity(intent);
						} else if (choices.get(which).indexOf(getString(R.string.bottle_txt_new_maybe_kown)) != -1) {
							Intent intent = new Intent(FriendFeedActivity.this, HomeActivity.class);
							AppContext.getInstance().setCurrentItem(2);
							AppContext.getInstance().setGotoMaybeKown(true);
							startActivity(intent);
						}
					}
				});
		builder.create().show();
	}
	
	public void pushNoticeUpdateUI() {
		if (newBottleFeedCount + newExchangeFeedCount + newMessageCount + newNoticeCount + newMaybeKownCount > 0) {
			bottomLayout.setVisibility(View.VISIBLE);
			contentView.setText(message_content);
			btnCount.setText(newBottleFeedCount + newExchangeFeedCount + newMessageCount + newNoticeCount + newMaybeKownCount  + "");
			userImageLoader.DisplayImage(message_avatar, avatarView, R.drawable.avatar_default_big);
		} else {
			bottomLayout.setVisibility(View.GONE);
		}
	}
	
	public class PushBroadcastReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			Bundle bundle = intent.getExtras();
			if (bundle != null) {
				newBottleFeedCount = bundle.getInt("bottle_feed_count");
				newExchangeFeedCount = bundle.getInt("exchange_feed_count");
				newMessageCount = bundle.getInt("message_count");
				newNoticeCount = bundle.getInt("notice_count");
				message_content = bundle.getString("message_content");
				message_avatar = bundle.getString("message_avatar");
				newMaybeKownCount = bundle.getInt("maybe_kown_count");
				pushNoticeUpdateUI();
			}
		}
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		try {
			if (receiver != null) {
				unregisterReceiver(receiver);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 选择菜单 弹出框
	 * @param choices 显示的菜单
	 */
	public void chooseAction(final String[] choices, final FriendFeedEntry feedEntry) {
		final Context dialogContext = new ContextThemeWrapper(this, android.R.style.Theme_Light);
	
		final ListAdapter adapter = new ArrayAdapter<String>(dialogContext,
				android.R.layout.simple_list_item_1, choices);

		final AlertDialog.Builder builder = new AlertDialog.Builder(
				dialogContext);
		builder.setTitle(R.string.system_info);
		builder.setSingleChoiceItems(adapter, -1,
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						doSelectChooseEvent(which, choices, feedEntry);
					}
				});
		builder.create().show();
	}
	
	
	public void doSelectChooseEvent(int which, String choics[], FriendFeedEntry feedEntry) {
		if (feedEntry == null) {
			return;
		} else {
			if (choics.length == 2) {
				if (which == 0) {
					Intent intent = new Intent(this, UserPhotoActivity.class);
					intent.putExtra("picid", feedEntry.getFeed_picid());
					startActivity(intent);
				} else {
					Intent intent = new Intent(this, UserSpaceActivity.class);
					intent.putExtra("ouid", feedEntry.getFeed_related_uid());
					startActivity(intent);
				}
			} else {
				if (feedEntry.getFeed_related_uid() != 0) {
					Intent intent = new Intent(this, UserSpaceActivity.class);
					intent.putExtra("ouid", feedEntry.getFeed_related_uid());
					startActivity(intent);
				} else {
					Intent intent = new Intent(this, UserPhotoActivity.class);
					intent.putExtra("picid", feedEntry.getFeed_picid());
					startActivity(intent);
				}
			}
		}
	}
}
